<!-- src/views/Login.vue -->
<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-header">
        <h1 class="login-title">欢迎回来</h1>
        <p class="login-subtitle">请输入您的账户信息</p>
      </div>

      <form @submit.prevent="handleLogin" class="login-form">
        <div class="form-group">
          <input v-model="loginForm.username" type="text" placeholder="账户" required class="form-input" />
        </div>

        <div class="form-group">
          <input v-model="loginForm.password" type="password" placeholder="密码" required class="form-input" />
        </div>

        <button type="submit" class="login-btn" :disabled="loading">
          {{ loading ? '登录中...' : '登录' }}
        </button>
      </form>

      <div v-if="errorMessage" class="error-message">
        {{ errorMessage }}
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'

const router = useRouter()

// 登录表单数据
const loginForm = reactive({
  username: '',
  password: '',
  remember: false
})

const loading = ref(false)
const errorMessage = ref('')

// 模拟用户数据
const validUsers = [
  { username: 'admin', password: '123456' },
  { username: 'user', password: '123456' }
]

// 登录处理
const handleLogin = async () => {
  // 重置错误信息
  errorMessage.value = ''

  // 表单验证
  if (!loginForm.username.trim() || !loginForm.password.trim()) {
    errorMessage.value = '请输入账户和密码'
    return
  }

  loading.value = true

  try {
    // 模拟登录API调用
    await new Promise(resolve => setTimeout(resolve, 1000))

    // 验证用户信息
    const isValidUser = validUsers.some(
      user => user.username === loginForm.username && user.password === loginForm.password
    )

    if (isValidUser) {
      // 登录成功
      console.log('登录成功:', loginForm.username)

      // 保存登录状态
      localStorage.setItem('isLoggedIn', 'true')
      localStorage.setItem('username', loginForm.username)


      if (window.api) {
        window.api.window.resize(1400, 900)
      }
      // 跳转到首页
      router.push('/Ai')

    } else {
      errorMessage.value = '账户或密码错误'
    }
  } catch (error) {
    errorMessage.value = '登录失败，请稍后重试'
    console.error('登录错误:', error)
  } finally {
    loading.value = false
  }
}

// 检查是否已登录（可选）
import { onMounted } from 'vue'

onMounted(() => {
  const isLoggedIn = localStorage.getItem('isLoggedIn')
  if (window.api) {
    window.api.window.resize(400, 550)
  }
  if (isLoggedIn === 'true') {
    // 如果已登录，直接跳转到首页
    router.push('/Ai')
  }
})
</script>

<style scoped lang="scss">
.login-container {
  min-height: 100vh;
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  // 渐变

  background-image: linear-gradient(to top, #48c6ef 0%, #6f86d6 100%);
  padding: 20px;
}

.login-box {
  background: white;
  padding: 48px 40px;
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  width: 100%;
  max-width: 400px;
  border: 1px solid #f1f5f9;
}

.login-header {
  text-align: center;
  margin-bottom: 40px;
}

.login-title {
  font-size: 28px;
  font-weight: 600;
  color: #1e293b;
  margin-bottom: 8px;
  letter-spacing: -0.5px;
}

.login-subtitle {
  font-size: 14px;
  color: #64748b;
  margin: 0;
}

.login-form {
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.form-group {
  display: flex;
  flex-direction: column;
}

.form-input {
  padding: 14px 16px;
  border: 1.5px solid #e2e8f0;
  border-radius: 8px;
  font-size: 15px;
  transition: all 0.2s ease;
  background: #ffffff;
  color: #1e293b;
}

.form-input::placeholder {
  color: #94a3b8;
}

.form-input:focus {
  outline: none;
  border-color: #3b82f6;
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
}

.form-input:hover {
  border-color: #cbd5e1;
}

.login-btn {
  padding: 14px;
  background: #3b82f6;
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 15px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s ease;
  margin-top: 8px;
}

.login-btn:hover:not(:disabled) {
  background: #2563eb;
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(59, 130, 246, 0.3);
}

.login-btn:active:not(:disabled) {
  transform: translateY(0);
}

.login-btn:disabled {
  background: #cbd5e1;
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
}

.error-message {
  margin-top: 20px;
  padding: 12px 16px;
  background: #fef2f2;
  border: 1px solid #fecaca;
  border-radius: 8px;
  color: #dc2626;
  text-align: center;
  font-size: 14px;
  font-weight: 500;
}

/* 响应式设计 */
@media (max-width: 480px) {
  .login-box {
    padding: 32px 24px;
    margin: 0 16px;
  }

  .login-title {
    font-size: 24px;
  }
}
</style>
